<?php

namespace App\Http\Controllers\Admin\Assistant;

use App\Http\Controllers\Admin\BaseController;
use App\Repositories\UserRepositoryEloquent;

use Breadcrumbs, Toastr,Config,Validator,DB;
use Illuminate\Http\Request;
use App\Http\Requests\Admin\Assistant\Article\CreateRequest;
use App\Http\Requests\Admin\Assistant\Article\UpdateRequest;
use App\Models\Article;
class ArticleController extends BaseController{

    public function __construct()
    {
       
    }

    public function index(Request $request){

        $where = [];

        $data=['title'=>'','from'=>'','to'=>''];

        if(!empty($request->all())){
            //if($request->name){
            //dd($request->all());
            $datas = $request->all();
            foreach($datas as $k=>$v){
                if($k=='title' && $v!=''){
                    $where[] = ["$k",'like','%'.$v.'%'];
                }
                if($k=='from' && $v!=''){
                    //$where[] = ['created_at','>',strtotime($v)];
                    $where[] = ['created_at','>',$v];
                }
                if($k=='to' && $v!=''){
                    //$where[] = ["created_at",'<',strtotime($v)];
                    $where[] = ["created_at",'<',$v];
                }
               
                $data[$k] =$v;
                
            }
            //dd($where);
            //$users = User::where($where)->paginate(20);
        }

        $articles =Article::where($where)->orderBy('sort','desc')->orderBy('id','desc')->paginate(20);
        //dd($smses);
        return view('admin.assistant.articles.index',compact('articles','data'));
    }

    public function create(){
        return view('admin.assistant.articles.create');
    }

    public function store(CreateRequest $request){

        $picurl = '';
        if($request->hasFile('picurl')){
            if ($request->file('picurl')->isValid()){
            //if (1){
                $images=$request->file('picurl'); //使用laravel 自带的request类来获取一下文件.
                $filedir="upload/admin/assistant/articles/"; //定义图片上传路径
                $imagesName=$images->getClientOriginalName(); //获取上传图片的文件名
                $extension = $images->getClientOriginalExtension();
                //$images->move($filedir,$imagesName); //使用move 方法移动文件.
                $newImagesName=md5(time()).random_int(5,5).".".$extension; //重新命名上传文件名字
                $images->move($filedir,$newImagesName); //使用move 方法移动文件.
                $picurl =$filedir.$newImagesName ;
            }else{
                Toastr::error('图标上传失败!');
                return redirect(route('admin.assistant.articles.create'));
            }
        }

        $result = Article::create([
            'title'=>$request['title'],
            'description'=>$request['description'],
            'content'=>$request['content'],
            'cover'=>$picurl,
            'status'=>$request['status'],
            'ishome'=>$request['ishome'],
            'popup'=>$request['popup']
            
        ]);
        if(!$result){
            Toastr::error('公告添加失败!');
            return redirect(route("admin.assistant.articles.create"));
        }
        Toastr::success('公告添加成功!');
         return redirect(route('admin.assistant.articles'));
        
    }
    
    public function edit($id){
        $article = Article::find($id);
        //dd($reply);
        return view("admin.assistant.articles.edit",compact('article'));
    }

    public function update(UpdateRequest $request, $id){
        //$type=$request['msgtype'];
        $data = [
            'title'=>$request['title'],
            'description'=>$request['description'],
            'content'=>$request['content'],
            'status'=>$request['status'],
            'ishome'=>$request['ishome'],
            'popup'=>$request['popup']
        ];

        if($request->hasFile('picurl')){
            if ($request->file('picurl')->isValid()){
            //if (1){
                $images=$request->file('picurl'); //使用laravel 自带的request类来获取一下文件.
                $filedir="upload/admin/assistant/articles/"; //定义图片上传路径
                $imagesName=$images->getClientOriginalName(); //获取上传图片的文件名
                $extension = $images->getClientOriginalExtension();
                //$images->move($filedir,$imagesName); //使用move 方法移动文件.
                $newImagesName=md5(time()).random_int(5,5).".".$extension; //重新命名上传文件名字
                $images->move($filedir,$newImagesName); //使用move 方法移动文件.
                $data['cover'] =$filedir.$newImagesName ;
            }else{
                Toastr::error('封面上传失败!');
                return redirect(route('admin.shop.option.edit'));
            }
        }

        //$data = $request->all();
        //dd($data);
        $isAble = Article::where('id', '<>', $id)->where('title', $data['title'])->count();
        if($isAble) {
           $result= [
                'status' => false,
                'msg' => '标题已被使用'
            ];
            Toastr::error($result['msg']);
        }else{
            $res = Article::where('id','=',$id)->update($data);
            if(!$res) {
                $result= [
                    'status' => false,
                    'msg' => '公告更新失败'
                ];
                Toastr::error($result['msg']);
            }else{
                Toastr::success('公告更新成功');
            }
            
        }

        // if(!$result['status']) {
        //         Toastr::error($result['msg']);
        // } else {
        //     Toastr::success('回复更新成功');
        // }

        
        return redirect(route('admin.assistant.articles.edit', ['id' => $id]));
        //return redirect('admin/wechat/reply');
    }

     public function destroy($id)
    {

        $article = Article::find($id);
        $result = $article->delete();
        return response()->json($result ? ['status' => 1] : ['status' => 0]);
    }

}